package wds

import (
	"fmt"
	"pool"
	"net/http"
	"encoding/json"
	"code.google.com/p/go-uuid/uuid"
	"labix.org/v2/mgo/bson"
)

func Addwd(w http.ResponseWriter, r *http.Request) {

	wdInfo := bson.M{}
	resD := bson.M{"ret": 0}
	// UUID
	wdInfo["_id"] = uuid.NewUUID().String()

	// UID
	cookie, _ := r.Cookie("uid");
	wdInfo["uid"] = cookie.Value;	

	r.ParseForm();

	if len(r.PostForm["wd"]) < 1 || len(r.PostForm["wdContent"]) < 1 {
		return
	}

	// Wd
	wdInfo["wd"] = r.PostForm["wd"][0]

	// WdContent
	wdInfo["wdContent"] = r.PostForm["wdContent"][0]

	// 数据入库
	db := <-pool.MongoDB
	c := db.C("words")
	err := c.Insert(wdInfo)
	if err != nil {
		// 数据插入数据库错误
		resD["ret"] = 2000
	} else {
		resD["wid"] = wdInfo["_id"]
	}
	pool.MongoDB <- db

	resB, _ := json.Marshal(resD)
	res := string(resB)
	fmt.Fprintf(w, "%s", res)
}















